package com.doublethread.easymart.modules.product.controller;

import com.doublethread.easymart.common.result.PageResult;
import com.doublethread.easymart.common.result.Result;
import com.doublethread.easymart.modules.product.dto.CategoryDTO;
import com.doublethread.easymart.modules.product.dto.CategoryQueryDTO;
import com.doublethread.easymart.modules.product.dto.CategoryUpdateStatusDTO;
import com.doublethread.easymart.modules.product.service.CategoryService;
import com.doublethread.easymart.modules.product.vo.CategoryVO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 管理端分类控制器
 * 处理管理员对分类的管理操作
 */
@Slf4j
@RestController
@RequestMapping("/api/admin/categories")
public class AdminCategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 获取分类列表（平铺展示）
     * 支持分页查询、关键词搜索和条件筛选
     */
    @GetMapping
    public Result<PageResult<CategoryVO>> getCategoryList(CategoryQueryDTO queryDTO) {
        log.info("管理端查询分类列表: keyword={}, status={}, level={}, parentId={}, current={}, size={}",
                queryDTO.getKeyword(), queryDTO.getStatus(), queryDTO.getLevel(),
                queryDTO.getParentId(), queryDTO.getCurrent(), queryDTO.getSize());

        PageResult<CategoryVO> pageResult = categoryService.getCategoryList(queryDTO);

        log.info("管理端查询分类列表成功: total={}, current={}",
                pageResult.getTotal(), pageResult.getCurrent());

        return Result.success(pageResult);
    }

    /**
     * 获取分类详情
     */
    @GetMapping("/{id}")
    public Result<CategoryVO> getCategoryById(@PathVariable Long id) {
        log.info("获取分类详情: id={}", id);

        CategoryVO categoryVO = categoryService.getCategoryById(id);

        log.info("获取分类详情成功: id={}", id);
        return Result.success(categoryVO);
    }

    /**
     * 添加分类
     */
    @PostMapping
    public Result<CategoryVO> createCategory(@Valid @RequestBody CategoryDTO categoryDTO) {
        log.info("创建分类: name={}, parentId={}", categoryDTO.getName(), categoryDTO.getParentId());

        CategoryVO categoryVO = categoryService.createCategory(categoryDTO);

        log.info("分类创建成功: id={}", categoryVO.getId());
        return Result.success(categoryVO);
    }

    /**
     * 更新分类信息
     */
    @PutMapping("/{id}")
    public Result<Void> updateCategory(@PathVariable Long id,
                                       @Valid @RequestBody CategoryDTO categoryDTO) {
        log.info("更新分类: id={}, name={}", id, categoryDTO.getName());

        categoryService.updateCategory(id, categoryDTO);

        log.info("分类更新成功: id={}", id);
        return Result.success();
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteCategory(@PathVariable Long id) {
        log.info("删除分类: id={}", id);

        categoryService.deleteCategory(id);

        log.info("分类删除成功: id={}", id);
        return Result.success();
    }

    /**
     * 更新分类状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateCategoryStatus(@PathVariable Long id,
                                             @Valid @RequestBody CategoryUpdateStatusDTO statusDTO) {
        log.info("更新分类状态: id={}, status={}", id, statusDTO.getStatus());

        categoryService.updateCategoryStatus(id, statusDTO);

        log.info("分类状态更新成功: id={}, status={}", id, statusDTO.getStatus());
        return Result.success();
    }


}
